//
//  JMUpView.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/31.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMUpView.h"
@interface JMUpView () <UIPickerViewDelegate, UIPickerViewDataSource>

@property (copy, nonatomic) NSString *number;
@property (weak, nonatomic) UIPickerView *pickerView;

@property (strong, nonatomic) NSArray *numberArr;
@property (assign, nonatomic) NSInteger integer;
@end
@implementation JMUpView
- (NSArray *)numberArr {
    if (!_numberArr) {
        _numberArr = @[@"0001", @"0010", @"0100", @"1000"];
    }
    return _numberArr;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - 44)]; // 44为下面按钮的高度
        self.pickerView = pickerView;
        [self addSubview:pickerView];
        pickerView.delegate = self;
        pickerView.dataSource = self;
        self.pickerView.backgroundColor = [UIColor whiteColor];
        
        // 取消按钮
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:cancelBtn];
        cancelBtn.frame = CGRectMake(-30, self.pickerView.height - 10, JMPhoneWidth * 0.5, 44);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:JMColor(153, 153, 153) forState:UIControlStateNormal];
        [cancelBtn setTitleColor:JMColor(153, 153, 153) forState:UIControlStateHighlighted];
        [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
        
        // 确定按钮
        UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:confirmBtn];
        confirmBtn.frame = CGRectMake(JMPhoneWidth * 0.5 + 30, cancelBtn.y, JMPhoneWidth * 0.5, 44);
        [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [confirmBtn setTitleColor:JMColor(77, 153, 203) forState:UIControlStateNormal];
        [confirmBtn setTitleColor:JMColor(77, 153, 203) forState:UIControlStateHighlighted];
        [confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
        self.backgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0];
        
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

/**
 *  向代理传送选择按钮的文字
 */
- (void)confirm {
    [UIView animateWithDuration:0.3 animations:^{
        self.y = JMPhoneHeight;
    }];
    if ([self.delegate respondsToSelector:@selector(upVIew:clickConfirmButton:)]) {
        [self.delegate upVIew:self clickConfirmButton:self.number];
    }
}

/**
 *  取消
 */
- (void)cancel {
    [UIView animateWithDuration:0.3 animations:^{
        self.y = JMPhoneHeight;
    }];
}

#pragma mark - PickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 4;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    if (self.integer != row) { //不是选中的行
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JMPhoneWidth, 40)];
    
        UILabel *label= [[UILabel alloc] init];
        label.frame = CGRectMake(0, 0, self.width, 40);
        label.text = [self.numberArr objectAtIndex:row];
        [view addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = JMColor(153, 153, 153);
        label.font = [UIFont systemFontOfSize:15];
    } else {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JMPhoneWidth, 40)];
        
        UILabel *label= [[UILabel alloc] init];
        label.frame = CGRectMake(0, 0, self.width, 40);
        label.text = [self.numberArr objectAtIndex:row];
        [view addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = JMColor(77, 153, 203);
        label.font = [UIFont systemFontOfSize:17];
    }
    return view;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [self.numberArr objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.integer = row;
    self.number = [self.numberArr objectAtIndex:row];
    [self.pickerView reloadAllComponents];
}
@end
